package com.example.yiwei.crm.view.activity.customerActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.dao.dockDao;
import com.example.yiwei.crm.dao_impl.dockDaoImpl;
import com.example.yiwei.crm.data.GetCustumer;
import com.example.yiwei.crm.data.GetTeamMember;
import com.example.yiwei.crm.entity.CustomerMessageEntity;
import com.example.yiwei.crm.entity.teamMemberEntity;
import com.example.yiwei.crm.util.NetWorkUtils;
import com.example.yiwei.crm.util.SC;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static com.example.yiwei.crm.R.id.add_team_member_spinner1;
import static com.example.yiwei.crm.R.id.add_team_member_spinner2;


public class AddTeamMemberActivity extends AppCompatActivity {
private Spinner spinner1,spinner2;
    private ImageView img_commit,img_back;
    SharedPreferences sp;
    private List<String> qx_list,member_list;
    private ArrayAdapter adapter,adapter6;
    List<teamMemberEntity> list;
    String team_member;
    int team_member_id;
    String qx_name;
   CustomerMessageEntity customerMessageEntity;
    int customer_id;
    String boolen;
    String json;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_team_member);
        spinner1= (Spinner) findViewById(add_team_member_spinner1);
        spinner2= (Spinner) findViewById(add_team_member_spinner2);
        img_back= (ImageView) findViewById(R.id.add_team_member_img_back);
        img_commit= (ImageView) findViewById(R.id.add_team_member_img_commit);
        list=new ArrayList<>();
        member_list=new ArrayList<>();
        sp = getSharedPreferences("login", Context.MODE_PRIVATE);
        customerMessageEntity= GetCustumer.get();
        customer_id=customerMessageEntity.getClient_id();
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        qx_list=new ArrayList<>();
        qx_list.add(0,"");
        qx_list.add(1,"读取");
        qx_list.add(2,"读取修改");
        qx_list.add(3,"读取修改删除");
        adapter6 = new ArrayAdapter(AddTeamMemberActivity.this, R.layout.spinner_display_style, R.id.txtvwSpinner, qx_list);
        adapter6.setDropDownViewResource(R.layout.spinner_dropdown_style);
        spinner2.setAdapter(adapter6);
        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                qx_name=spinner2.getSelectedItem().toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        new Thread(new Runnable() {
            @Override
            public void run() {
                dockDao dockDao=new dockDaoImpl();
                SC sc = new SC();
                JSONObject jsonObject = sc.storage(sp);
                String json=jsonObject.toString();
                list=dockDao.queryAddTeamMemberSpinner(baseClass.query_team_member_spinner_url,json);
                for (int i2=0;i2<list.size();i2++){
                   member_list.add(list.get(i2).getTeam_member_name());
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter=new ArrayAdapter(AddTeamMemberActivity.this, R.layout.spinner_display_style, R.id.txtvwSpinner, member_list);
                        adapter.setDropDownViewResource(R.layout.spinner_dropdown_style);
                        spinner1.setAdapter(adapter);
//                        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
//                            @Override
//                            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
//                                managerName = spinner_manager.getSelectedItem().toString();
//                                for (int i1 = 0; i1 < managerList.size(); i1++) {
//                                    if (managerName.equals(managerList.get(i1).getManager_name())) {
//                                        manager_id = Integer.valueOf(managerList.get(i1).getManager_id());
//                                    }
//                                }
//                                if ("update".equals(getIntent().getStringExtra("tag"))) {
//                                    showMessage();
//                                }
//                            }
//                        }
                        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                team_member=spinner1.getSelectedItem().toString();
                                for (int i=0;i<list.size();i++){
                                    if (team_member.equals(list.get(i).getTeam_member_name())){
                                        team_member_id=Integer.valueOf(list.get(i).getTeam_member_id());
                                    }
                                }

                            }

                            @Override
                            public void onNothingSelected(AdapterView<?> parent) {

                            }
                        });
                    }
                });
            }
        }).start();
        img_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        SC sc = new SC();
                        JSONObject jsonObject = sc.storage(sp);
                        try {
                            jsonObject.put("KAA01",customer_id);
                            jsonObject.put("UAB01L",team_member_id);
                            jsonObject.put("UAB03L",team_member);
                            if (qx_name.equals("读取")){
                                jsonObject.put("KTA03",0);
                            }else if (qx_name.equals("读取修改")){
                                jsonObject.put("KTA03",1);
                            }else if (qx_name.equals("读取修改删除")){
                                jsonObject.put("KTA03",2);
                            }
                            jsonObject.put("addoredit","add");
                            jsonObject.put("CID03",sp.getString("CID03",null));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        String json=jsonObject.toString();
                        String result= NetWorkUtils.getCount(baseClass.add_team_member_url,json);
                        try {
                            JSONObject jsonObject1=new JSONObject(result);
                            boolen=jsonObject1.getString("ifsuccess");
                                    if ("true".equals(boolen)){
                                        finish();
                                    }else{
                                       runOnUiThread(new Runnable() {
                                           @Override
                                           public void run() {
                                               Toast.makeText(AddTeamMemberActivity.this, "添加失败！", Toast.LENGTH_SHORT).show();
                                           }
                                       });
                                    }


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }).start();
            }
        });
    }
}
